package com.codeoss.framework.view.pagebean.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.view.component.Formula;
import com.codeoss.framework.view.component.IDataBindSource;
import com.codeoss.framework.view.component.IDataBinding;
import com.codeoss.framework.view.component.IVarName;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.RollUpSummary;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.datasource.IDataSource;
import com.codeoss.framework.view.datasource.store.IDataSourceStore;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.framework.view.util.DataSourceUtil;

@Service
public class BaseFormViewPageBean extends BasePageBean {
	public static final String KEY_FORM_FIELD_LIST = "key_form_field_list";
	@Autowired
	private IVariableService variableService;
	@Autowired
	private IScriptServiceStore scriptServiceStore;
	@Autowired
	private IDataSourceStore dataSourceStore;
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private DataSourceUtil dataSourceUtil;
	public void saveParentFormulaAndSummaryField() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
        boolean needSave=false;
		List<IDataBinding> formDataBindingList =page.getAttribute(KEY_FORM_FIELD_LIST);
		for(int i=0;i<formDataBindingList.size();i++) {
			IDataBinding dataBinding=formDataBindingList.get(i);
			if(dataBinding instanceof RollUpSummary) {
				RollUpSummary rollUpSummary=(RollUpSummary)dataBinding;
				Expression valueExpression=rollUpSummary.getValueExpression();
				if(valueExpression!=null) {
					Object value=scriptServiceStore.getScriptService(valueExpression).eval(valueExpression.getScript(), null);
					variableService.setVariableValue(yunContext, rollUpSummary.getVarName(), value);
					needSave=true;
				}
			}
		}
		if(needSave) {
			for(int i=0;i<formDataBindingList.size();i++) {
				IDataBinding dataBinding=formDataBindingList.get(i);
				if(dataBinding instanceof Formula) {
					Formula formula=(Formula)dataBinding;
					Expression expression=formula.getFormulaExpression();
					Object value=scriptServiceStore.getScriptService(expression).eval(expression.getScript(), yunContext.getVarValueMap());
					variableService.setVariableValue(yunContext, formula.getVarName(), value);
				}
			}
			DataSourceConfig mainDataSourceConfig = dataSourceUtil.getDataSourceConfigById(yunContext.getPage(),
					((IDataBindSource)formDataBindingList.get(0)).getDataSource());
			Table table=tableStore.get(mainDataSourceConfig.getTableName());
			IDataSource dataSource=dataSourceStore.getDataSource(table.getAccessType());
			Map<String, Object> dataMap=getSubmitFormData();
			dataSource.update(yunContext, table, dataMap);
		}
	}
	
	protected Map<String, Object> getSubmitFormData(){
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		List<IDataBinding> formDataBindingList = page.getAttribute(KEY_FORM_FIELD_LIST);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		for (int i = 0; i < formDataBindingList.size(); i++) {
			IDataBinding dataBinding = formDataBindingList.get(i);
			String varName=((IVarName)dataBinding).getVarName();
			if(variableService.containVariable(yunContext, varName)) {
				Object value = variableService.getVariableValue(yunContext,varName);
				dataMap.put(dataBinding.getDataField(), value);
			}	
		}
		return dataMap;
	}
	
	public Object getCurrentKeyValue() {
		Map<String,Object> map=getSubmitFormData();
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		List<IDataBinding> formDataBindingList =page.getAttribute(KEY_FORM_FIELD_LIST);
		DataSourceConfig mainDataSourceConfig = dataSourceUtil.getDataSourceConfigById(yunContext.getPage(),
				((IDataBindSource)formDataBindingList.get(0)).getDataSource());
		Table table=tableStore.get(mainDataSourceConfig.getTableName());
		Column keyColumn=table.getPrimaryKeyColumn();
		return map.get(keyColumn.getColumnName());
	}
}
